package com.df.barcodescaning.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.df.barcodescaning.MyApplication;
import com.df.barcodescaning.R;

/**
 * Created by G on 2018/6/1.
 */

public abstract class BaseActivity extends AppCompatActivity{

    protected boolean isNeedBackKey = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(isNeedBackKey && getSupportActionBar()!=null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);  //返回键
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_base, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.options_logout:
                MyApplication.getInstance().deleteLoggedUser();
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
